﻿using DapperExtensions;
using DapperExtensions.Mapper;
using DapperExtensions.Sql;
using WMLogistics.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using Dapper;
using WMLogistics.Model.Response;

namespace WMLogistics_Dao
{
    public class CompanyNoticeDao
    {
        private SqlGeneratorImpl sqlGenerator;
        public CompanyNoticeDao()
        {
            var config = new DapperExtensionsConfiguration(typeof(AutoClassMapper<>), new List<Assembly>(), new MySqlDialect());
            sqlGenerator = new SqlGeneratorImpl(config);
            DapperExtensions.DapperExtensions.Configure(config);
        }

        public wuliu_company_notice Get(int id)
        {
            using (var conn = ConnectionHelper.OpenConnection())
            {
                return conn.Get<wuliu_company_notice>(id);
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteNotice(wuliu_company_notice model)
        {

            using (var conn = ConnectionHelper.OpenConnection())
            {
                StringBuilder str = new StringBuilder();
                StringBuilder strSql = new StringBuilder();
                strSql.Append("update wuliu_company_notice set ");
                if (model.company_no.IfNotNull())
                    strSql.Append("company_no=@company_no,");
                if (model.notice_id.IfNotNull())
                    strSql.Append("notice_id=@notice_id,");
                if (model.title.IfNotNull())
                    strSql.Append("title=@title,");
                if (model.release_time.IfNotNull())
                    strSql.Append("release_time=@release_time,");
                if (model.iread.IfNotNull())
                    strSql.Append("iread=@iread,");
                if (model.read_time.IfNotNull())
                    strSql.Append("read_time=@read_time,");
                if (model.idelete.IfNotNull())
                    strSql.Append("idelete=@idelete,");
                if (model.delete_time.IfNotNull())
                    strSql.Append("delete_time=@delete_time,");
                if (model.Operator.IfNotNull())
                    strSql.Append("operator=@operator,");
                if (model.operator_time.IfNotNull())
                    strSql.Append("operator_time=@operator_time,");
                if (model.notice_type.IfNotNull())
                    strSql.Append("notice_type=@notice_type");
                str.Append(strSql.ToString().TrimEnd(','));
                str.Append(" where id=@id ");
                return conn.Execute(str.ToString(), model) > 0;
            }
        }

        public PageData<wuliu_company_notice> GetPageList(IPredicate predicate, IList<ISort> sorts, int pageIndex, int pageSize)
        {
            using (var conn = ConnectionHelper.OpenConnection())
            {
                var pageData = new PageData<wuliu_company_notice>();
                pageData.Items = conn.GetPage<wuliu_company_notice>(predicate, sorts, pageIndex, pageSize);
                pageData.TotalNum = conn.Count<wuliu_company_notice>(predicate);
                pageData.CurrentPage = pageIndex;
                pageData.TotalPageCount = pageData.TotalNum % pageSize == 0 ? pageData.TotalNum / pageSize : pageData.TotalNum / pageSize + 1;

                return pageData;
            }
        }

        public int Add(wuliu_company_notice company_notice)
        {
            using (var conn = ConnectionHelper.OpenConnection())
            {
                return conn.Insert<wuliu_company_notice>(company_notice);
            }
        }

        public int Add(wuliu_wlcompany_notice company_notice)
        {
            using (var conn = ConnectionHelper.OpenConnection())
            {
                return conn.Insert<wuliu_wlcompany_notice>(company_notice);
            }
        }
    }
}
